$(function(){
    //获取用户输入的内容
    $("#username").on("blur",function(){
        var username = $("#username").val();
        if(username.trim() == ""){
            mui.toast("收货人姓名不能为空");
            return;
        }
    });
    //验证邮编
    $("#code").on("blur",function(){
        var code = $("#code").val();
        var reg = /^[1-9]\d{5}(?!\d)$/;
        if(code.trim() == ""){
            mui.toast("邮编不能为空");
            return;
        } 
        if(!reg.test(code)){
            mui.toast("邮编格式不正确");
            return;
        }
    });

    //城市三级联动
    //创建picker
    var picker = new mui.PopPicker({layer:3}); 
    //设置
    picker.setData(cityData); 
    $("#city").on("tap",function(){
        picker.show(function (selectItems) {
            console.log(selectItems);
            $("#city").val(selectItems[0].text+selectItems[1].text+selectItems[2].text);
        })
    });

    //添加地址事件
    $("#add-btn").on("click",function(){
        //获取页面中的内容
        // var info = $("#formAdress").serialize();
        var username = $("#username").val();
        var code = $("#code").val();
        // console.log(code);
        var city = $("#city").val();
        var adress = $("#adress").val();
        // console.log(info);
        $.ajax({
            type:"post",
            url:"/address/addAddress",
            data:{
                "recipients" :username,
                "postcode":code,
                "address":city,
                "addressDetail":adress
            },
            dataType:"json",
            success:function(res){
                console.log(res);
                if(res.success){
                    mui.toast("添加地址成功");
                    setTimeout(function(){
                        location.href = "address.html";
                    },1000);
                    
                }
            }
        });
    });
    
});